package com.blogs.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;


import com.blogs.entity.AppConfig;
import com.blogs.entity.IngComFeed;
import com.blogs.entity.MsgEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GetIngComLine {
	public interface IMsgCallBack {
		public void onSuccess(ArrayList<IngComFeed> feedList);

		public void onFailed();
	}
	private String url = AppConfig.GET_ING_COM_LINE;
	private IMsgCallBack callback;
	private GetMsgTask timeLine;
	public GetIngComLine(String sid,String ingid, IMsgCallBack callback) {
		this.callback = callback;
		timeLine = new GetMsgTask(taskCallBack);
		url = url.replace("{0}", sid).replace("{1}", ingid);
	}
	public void loadData() {
		timeLine.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack = new GetMsgTask.onTaskCallBack() {
		public void onSuccess(String result)
		{
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<ArrayList<Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			ArrayList<IngComFeed> feeds=new ArrayList<IngComFeed>();
			Iterator<Object> iterator = msg.data.iterator();
			int i=0;
			while (iterator.hasNext()) 
			{
				i++;
				Map<String, Object> objs = (Map<String, Object>) iterator.next();
				IngComFeed feed = new IngComFeed(objs);
				feeds.add(feed);
			}
			callback.onSuccess(feeds);
		};
		public void onFailed() {
			callback.onFailed();
		};
	};
}
